﻿#region Mortgage Calculator UI Control
//
// All rights are reserved. Reproduction or transmission in whole or in part, in 
// any form or by any means, electronic, mechanical or otherwise, is prohibited 
// without the prior written consent of the copyright owner.
// 
// Filename: MortgageCalculator.xaml.cs
//
#endregion 
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.DataServices.DataSources;
using DiscoveryLogic.UI.Layouts.Controllers;
using DiscoveryLogic.UI.Layouts.Controls;
using DiscoveryLogic.UI.Layouts.Controls;
using System.Threading;
using System.ComponentModel;
using DiscoveryLogic.UI.Layouts.Converters;
using DiscoveryLogic.UI.Layouts.DataAccesses;

namespace DiscoveryLogic.UI.Layouts.ContentScreens
{
    /// <summary>
    /// User control to display users Mortgage account details and tools for 
    /// visualizing payment changes to amoritization schedule (View)
    /// </summary>
    public partial class MortgageCalculator : UserControl
    {
        /// <summary>
        /// Holder for current mortgage (Model)
        /// </summary>
        private MortgageCalcModel _mortgage;

        /// <summary>
        /// Constructor
        /// </summary>
        public MortgageCalculator()
        {
            InitializeComponent();

            _mortgage = new MortgageCalcModel(MortgageManager.GetMortgage(1));
            _mortgage.SuspendCalculation();

            Canvas can = MortgageCalculatorCanvas;
            TextBlock leftBlock = (TextBlock)can.FindName("LeftGroup");
            TextBlock middleBlock = (TextBlock)can.FindName("MiddleGroup");
            TextBlock rightBlock = (TextBlock)can.FindName("RightGroup");

            leftBlock.Text = String.Format((string)Resources["Res_LeftBlock"], _mortgage.Account.Balance.ToCurrency(), (_mortgage.Account.InterestRate / 100).ToPercentage(), Environment.NewLine);
            middleBlock.Text = String.Format((string)Resources["Res_MiddleBlock"], _mortgage.Account.NextPaymentDueDate.ToShortDateString(), _mortgage.Account.NextPaymentAmount.ToCurrency(), Environment.NewLine);
            rightBlock.Text = String.Format((string)Resources["Res_RightBlock"], _mortgage.Account.AmountPastDue.ToCurrency());

            _graphSlider.BalanceLegend = string.Format(this.Resources["Res_BalanceLegend"].ToString(), Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol);

            this.DataContext = _mortgage;

            //The data binding will cause the data change event to fire which recalcs the values.
            this.Loaded += (s, e) =>
            {
                _mortgage.ResumeCalculation();
                _mortgage.CalculateSavings();
            };
            _mortgage.ResumeCalculation();
            _mortgage.CalculateSavings();
        }

        #region Mouse Events

            protected void MenuLink_MouseEnter(object sender, EventArgs e)
            {
                ((UIElement)sender).Opacity = 1;
                ((FrameworkElement)sender).Cursor = Cursors.Hand;
            }

            protected void MenuLink_MouseLeave(object sender, EventArgs e)
            {
                ((UIElement)sender).Opacity = 0.7;
                ((FrameworkElement)sender).Cursor = Cursors.Arrow;
            }

        #endregion

    }


   
}
